package com.example.multinetty.netty.secondprotocal;

import com.example.multinetty.netty.send.ChannelSender;
import com.example.multinetty.netty.session.LocalSession;
import com.example.multinetty.netty.session.ServerSession;
import io.netty.channel.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.concurrent.ConcurrentHashMap;


@RestController
@RequestMapping(value = "/")
@Slf4j
public class LLFNettyUserController {
    private static final ConcurrentHashMap<Integer, ServerSession> onlineSessions = new ConcurrentHashMap<>();
    //此方法用于远程调用，需要加上Remote注解
    //@RequestMapping(value="/llflogin",method= RequestMethod.POST,produces = "text/html; charset=utf-8")
    @RequestMapping(value="/llflogin",method= RequestMethod.POST,produces = "application/json; charset=utf-8")
    public @ResponseBody NettyMessage llflogin(@RequestBody NettyMessage nettyMessage) {
        //此处并未访问数据库，只是做简单输出
        //System.out.println("客户端请求的用户id为======" + request.getAttribute("channel"));
        System.out.println("客户端请求的用户id为======" + nettyMessage.toString());
       /*LocalSession localSession=(LocalSession)request.getAttribute("channel");
        Channel channel=  localSession.getChannel();
//        channel.writeAndFlush("session back");
        onlineSessions.put(1,localSession);*/
        NettyMessage heartBeat=new NettyMessage();
        NettyMessage loginresponse=new NettyMessage();

        // 返回心跳应答消息
        /*if (nettyMessage.getHeader() != null
                && nettyMessage.getHeader().getType() == MessageType.HEARTBEAT_REQ
                .value())*/
            if (nettyMessage.getHeader() != null
                    && nettyMessage.getHeader().getType() == MessageType.LOGIN_REQ
                    .value())
            {
            log.info("Receive client heart beat message : ---> "
                    + nettyMessage);
             //heartBeat = buildHeatBeat();
                loginresponse=buildLoginReq();
            log.info("Send heart beat response message to client : ---> "
                    + heartBeat);
            //ctx.writeAndFlush(heartBeat);
            //return "响应结果===用户张三" + nettyMessage.toString();

        }
        return loginresponse;
    }
    private NettyMessage buildLoginReq() {
        NettyMessage message = new NettyMessage();
        Header header = new Header();
        header.setType(MessageType.LOGIN_RESP.value());
        message.setHeader(header);
        message.setBody((byte)0);
        return message;
    }
        private NettyMessage buildHeatBeat() {
            NettyMessage message = new NettyMessage();
            Header header = new Header();
            header.setType(MessageType.LOGIN_RESP.value());
            message.setHeader(header);
            message.setBody((byte)0);
            return message;
        }
  /*  @RequestMapping(value = "/sendback",method = RequestMethod.GET)
    public void sendback(){
        String message="hello,welcome";
        System.out.println("message ："+message);
        Channel channel=( (LocalSession) onlineSessions.get(1)).getChannel();
        System.out.println("Channel ："+channel.toString());
        new ChannelSender(channel).sendback(message);

    }*/
}